package com.testmall.entity.common;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author LittleDesire
 * @description
 * @date 2022-05-09 周一 14:40
 * @since 17
 */
@ConfigurationProperties(prefix = "mall.token")
public class TokenProperties {

    /**
     * 默认token失效时间:60m
     */
    final int DEFAULT_VALIDATE_TIME = 60;
    /**
     * 是否开启token验证功能，默认为开启状态
     */

    boolean enable = true;
    /**
     * 不拦截的路径
     */
    List<String> excludePathPatterns;
    /**
     * 要拦截的路径
     */
    List<String> pathPatterns;

    /**
     * token生成秘钥
     */
    String secret;

    /**
     * token有效时间，单位为分钟
     */
    int validateTime;

    public int getValidateTime() {
        if (this.validateTime == 0) {
            this.validateTime =DEFAULT_VALIDATE_TIME;
        }
        return validateTime;
    }

    public void setValidateTime(int validateTime) {
        this.validateTime = validateTime;
    }

    public boolean isEnable() {
        return enable;
    }

    /**
     * 如果不配置，则会生成一个uuid作为秘钥
     * @return
     */
    public String getSecret() {
        if (null == this.secret) {
            this.secret = UUID.randomUUID().toString();
        }
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }
    public boolean getEnable(){
        return enable;
    }

    /**
     * @return 不拦截的请求地址
     */
    public List<String> getExcludePathPatterns() {
        if (null == this.excludePathPatterns) {
            this.excludePathPatterns = new ArrayList<>();
        }
        return excludePathPatterns;
    }

    public void setExcludePathPatterns(List<String> excludePathPatterns) {
        this.excludePathPatterns = excludePathPatterns;
    }

    /**
     * 若没有配置要拦截的路径，则有一个默认的拦截所有请求地址
     * @return 拦截地址
     */
    public List<String> getPathPatterns() {
        if (null == this.pathPatterns) {
            this.pathPatterns = new ArrayList<>();
            this.pathPatterns.add("/**");
        }
        return pathPatterns;
    }

    public void setPathPatterns(List<String> pathPatterns) {
        this.pathPatterns = pathPatterns;
    }
}
